package benny_driving_ser.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import benny_driving_ser.ObjectBean.BonusInfo;
import benny_driving_ser.dao.DriRandomBonusDao;
import benny_driving_ser.dao.impl.DriRandomBonusImpl;
import benny_driving_ser.util.DBUtil;
import benny_driving_ser.util.JSONUtil;
import benny_driving_ser.util.SQL2ObjectListUtil;

public class RandomBonusAction extends BaseAction{

	private int orderid;
	String bonus;
	
	PrintWriter out = null;
	DriRandomBonusDao dao = null; 
	
	
	public String RandomBonus(){
	
		int stateNum;
		try{
			request.setCharacterEncoding("utf-8");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			response.setCharacterEncoding("utf-8");
			dao = new DriRandomBonusImpl();
			out = response.getWriter();
			String ip = getRemortIP(request);
			
			stateNum= 0;
			out.print(stateNum);
			out.flush();
			out.close();
			if(stateNum==0){
				return "success";
			}else{
				return "fail";
			}
			
		}catch(IOException e){ 
			e.printStackTrace();
		}
		return "success";
	}
	
	//加载最近十条中奖信息
	@SuppressWarnings("unchecked")
	public String loadBonusInfos(){
		
		List<BonusInfo> list = new ArrayList<BonusInfo>();
		String sql_query_bonusinfo = "select id,orderid,driid,time,bonus_cost from t_bonus_info where bonus_cost !=0 order by time desc limit 0,10";
		String bonusInfos = "bonuInfos";
		BonusInfo b = new BonusInfo();
		Map<String,String> map = new HashMap<String,String>();
		
		try{
			map.put("id", "id");
			map.put("orderId", "orderid");
			map.put("driId", "driid");
			map.put("time", "time");
			map.put("bonusCost", "bonus_cost");
			
			request.setCharacterEncoding("utf-8");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			list = (List<BonusInfo>) SQL2ObjectListUtil.sql2ObjectList(b, sql_query_bonusinfo, map);
			String json = JSONUtil.object2json(bonusInfos, list);
			out.print(json);
			out.flush();
			out.close();
			return "success";
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
		return "fail";
	}
	
	//根据司机Id加载中奖信息
	public String loadBonusInfoById(int driId){
		
		List<BonusInfo> list = new ArrayList<BonusInfo>();
		String sql_query_bonusinfo = "select id,orderid,driid,time,bonus_cost from t_bonus_info where bonus_cost !=0 order by time desc limit 0,10";
		String bonusInfos = "bonuInfos";
		BonusInfo b = new BonusInfo();
		Map<String,String> map = new HashMap<String,String>();
		
		try{
			map.put("id", "id");
			map.put("orderId", "orderid");
			map.put("driId", "driid");
			map.put("time", "time");
			map.put("bonusCost", "bonus_cost"); 
			
			request.setCharacterEncoding("utf-8");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			list = (List<BonusInfo>) SQL2ObjectListUtil.sql2ObjectList(b, sql_query_bonusinfo, map);
			String json = JSONUtil.object2json(bonusInfos, list);
			out.print(json);
			out.flush();
			out.close();
			return "success";
			
		}catch(IOException e){
			e.printStackTrace();
		}
		
		
		return "fail";
	}
	
	
	private String getRemortIP(HttpServletRequest request) {

		if (request.getHeader("x-forwarded-for") == null) {
			
		return request.getRemoteAddr();
		}
		return null;
	
	}
	
	public int getOrderid() {
		return orderid;
	}

	public void setOrderid(int orderid) {
		this.orderid = orderid;
	}

	public PrintWriter getOut() {
		return out;
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}

	public DriRandomBonusDao getDao() {
		return dao;
	}

	public void setDao(DriRandomBonusDao dao) {
		this.dao = dao;
	}
	
}
